package formBean;

import java.util.ArrayList;

import org.mybeans.form.FormBean;

import util.Util;

public class DepositCheckForm extends FormBean{
	private String customerName;
	private String amountString;
	private long amount;
	
	public String getCustomerName() {
		return customerName;
	}
	
	public void setCustomerName(String customerName) {
		this.customerName = trimAndConvert(customerName,"<>\"");
	}
	
	public String getAmountString() {
		return amountString;
	}
	
	public void setAmountString(String amountString) {
		this.amountString = trimAndConvert(amountString,"<>\"");
	}
	
	public long getAmount() {
		return amount;
	}
	
	public ArrayList<String> getValidationErrors() {
		ArrayList<String> errors = new ArrayList<String>();
		
		if (customerName == null || customerName.length() == 0) {
			errors.add("Customer username is required.");
		}
		
		if (amountString == null || amountString.length() == 0) {
			errors.add("Amount is required");
		} else if (!Util.isValidMoneyAmount(this.amountString)) {
			errors.add("The amount should be less than 1,000,000 with only two decimal places.");
		} 
		
		if (errors.size() > 0) return errors;		
		
		try {
			this.amount = (long) (Double.valueOf(amountString) * 100);
			if (this.amount <= 0) {
				errors.add("The amount should be greater than 0.");
			} 
		} catch (NumberFormatException e) {
			errors.add("The amount should be a number that tracks to two decimal places.");
		}		
		
		if (customerName.matches(".*[<>\"].*")) errors.add("Customer name may not contain angle brackets or quotes");
		
		return errors;
	}
}
